//
//  ViewController.m
//  CoreLocation
//
//  Created by xbq on 14-6-24.
//  Copyright (c) 2014年 zhx. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate>
@property(nonatomic,strong)UILabel *statusLabel;
@property (strong, nonatomic)CLLocationManager *locationManager;
@property(strong, nonatomic) CLBeaconRegion *beaconRegion;
@property (strong, nonatomic) NSString *currentUDID;
@end

@implementation ViewController
            
- (void)viewDidLoad {
    [super viewDidLoad];
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.beaconRegion= [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D"] identifier:@"est"];

    self.beaconRegion.notifyOnEntry=YES;
    self.beaconRegion.notifyOnExit=YES;
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
        [self.locationManager requestAlwaysAuthorization];
    } 

    self.beaconRegion.notifyEntryStateOnDisplay=YES;
    [self.locationManager startMonitoringForRegion:self.beaconRegion];
    
    self.statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 80, 200,100)];
    self.statusLabel.text =@"1";
    [self.view addSubview:self.statusLabel];
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
    if ([beacons count]>0) {
        for (CLBeacon *beacon in  beacons) {
            if (![self.currentUDID isEqualToString:beacon.proximityUUID.UUIDString]) {
                NSLog(@"UDID = %@",beacon.proximityUUID.UUIDString);
            }
            self.currentUDID =beacon.proximityUUID.UUIDString;
        }
    }
}

- (void) locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
    [self.locationManager requestStateForRegion:self.beaconRegion];
}

- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
    self.statusLabel.text=[error localizedDescription];

    NSLog(@"Region monitoring failed with error: %@", [error localizedDescription]);
}

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    /*
     A user can transition in or out of a region while the application is not running. When this happens CoreLocation will launch the application momentarily, call this delegate method and we will let the user know via a local notification.
     */
    if (state == CLRegionStateInside)
    {
        //Start Ranging
        [manager startRangingBeaconsInRegion:self.beaconRegion];
    }
    else
    {
        //Stop Ranging here
    //    [manager stopMonitoringForRegion:self.beaconRegion];

    }
    
    UILocalNotification *notification = [[UILocalNotification alloc] init];
   // notification.region = region;
    if(state == CLRegionStateInside)
    {
        notification.alertBody = @"进入ibeacon";
        NSLog(@"进入iBeacon");
        self.statusLabel.text=@"进入iBeacon";

    }
    else if(state == CLRegionStateOutside)
    {
        notification.alertBody = @"离开ibeacon";
        NSLog(@"离开iBeacon");
        self.statusLabel.text=@"离开iBeacon";

    }
    else
    {
        return;
    }
    
    /*
     If the application is in the foreground, it will get a callback to application:didReceiveLocalNotification:.
     If it's not, iOS will display the notification to the user.
     */
     [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{


}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    self.statusLabel.text=@"Entered region";
}

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    self.statusLabel.text=@"Exited region";
}

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations{


    NSLog(@"+%@",locations);
}
@end
